package com.example.warehouse_manager.controller;

import com.example.warehouse_manager.dto.AssignRoleDto;
import com.example.warehouse_manager.entity.CurrentUser;
import com.example.warehouse_manager.entity.Result;
import com.example.warehouse_manager.entity.User;
import com.example.warehouse_manager.entity.UserRole;
import com.example.warehouse_manager.page.Page;
import com.example.warehouse_manager.service.RoleService;
import com.example.warehouse_manager.service.UserRoleService;
import com.example.warehouse_manager.service.UserService;
import com.example.warehouse_manager.utils.TokenUtils;
import com.example.warehouse_manager.utils.WarehouseConstants;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private RoleService roleService;

    //分页查询用户
    @RequestMapping("/user-list")
    public Result userList(Page page,User user){
        page = userService.queryUserByPage(page, user);
        return Result.ok(page);
    }

    //添加用户
    @RequestMapping("/addUser")
    public Result addUser(@RequestBody User user,@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){

        //拿到当前登录用户的id
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int createBy = currentUser.getUserId();
        user.setCreateBy(createBy);

        return userService.saveUser(user);

    }

    //禁用、启用用户
    @RequestMapping("/updateState")
    public Result updateState(@RequestBody User user){
        return userService.updateState(user);
    }

    //查询用户已分配的角色
    //@PathVariable Integer userId表示将路径占位符userId的值赋值给请求处理方法入参变量userId
    @RequestMapping("/user-role-list/{userId}")
    public Result userRoleList(@PathVariable Integer userId){
        return Result.ok(roleService.queryUserRoleById(userId));
    }

    //给用户分配角色
    @RequestMapping("/assignRole")
    public Result assignRole(@RequestBody AssignRoleDto assignRoleDto){
        userService.assignRole(assignRoleDto);
        return Result.ok("分配角色成功");

    }

    //根据用户id删除单个用户的url接口
    @RequestMapping("/deleteUser/{userId}")
    public Result deleteUserById(@PathVariable Integer userId){
        //Collections.singletonList()这个List中只能存放一个元素，多一个或者少一个都会导致异常。
        //return userService.removeUserById(Arrays.asList(userId));
        return userService.removeUserById(Collections.singletonList(userId));
    }

    //根据用户ids批量删除用户的url接口
    @RequestMapping("/deleteUserList")
    public Result deleteUserByIds(@RequestBody List<Integer> userIdList){
        return userService.removeUserById(userIdList);
    }

    //修改用户
    @RequestMapping("/updateUser")
    public Result updateUser(@RequestBody User user,@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        //拿到当前登录用户的id
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int userId = currentUser.getUserId();

        user.setUpdateBy(userId);

        //执行业务
        return userService.setUserById(user);
    }

    //重置密码
    @RequestMapping("/updatePwd/{userId}")
    public Result resetPassword(@PathVariable Integer userId){
        return userService.setPwdById(userId);
    }

    //导出数据
    @RequestMapping("/exportTable")
    public Result exportTable(Page page,User user){
        page = userService.queryUserByPage(page, user);
        List<?> resultList = page.getResultList();
        //响应
        return Result.ok(resultList);
    }

}
